package com.cunyme;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.OpenableColumns;
import android.widget.Toast;

public class helper_functions{

	public Map<String, String> getcookies(String category, SQLiteDatabase db)
	{
		// Open up the database again.
		String[] result_columns = new String[] {"category", "cookies"};
		
		// Create a variable to store the portal cookies in...
		Map <String, String> cookies = new HashMap<String, String>();
		
		// We store all cookie data in the "allRows" cursor.  Then, we move the cursor to the top,
		// and check every row until we find the row with the category label "portal."
		Cursor allRows = db.query("cookies_table", result_columns, null, null, null, null, null);
		allRows.moveToFirst();
		String cookie_category = allRows.getString(0);
		while (!cookie_category.equals(category))
		{
			allRows.moveToNext();
			if (allRows.getString(0) == null)
			{
				cookies.put("Error", "No cookies found");
				return cookies;
			}	
			cookie_category = allRows.getString(0);
		}	
		
		String[] portal_cookie_elements = allRows.getString(1).split(",");
		String[] key_value_pair;
		for (String cookie_element:portal_cookie_elements)
		{
			// The cookie blob has a few brackets and spaces we need to get rid of, which is what the replace commands do.
			cookie_element = cookie_element.replace("{", "");
			cookie_element = cookie_element.replace("}", "");
			cookie_element = cookie_element.replace(" ", "");
			
			// Each cookie consists of the cookie name and the cookie data, divided by the equals sign.  We split the two
			// up into a key value pair, which we insert into the map.
			key_value_pair = cookie_element.split("=");
			cookies.put(key_value_pair[0], key_value_pair[1]);
		}
		return cookies;
	}
}
